
using ServiceStack.Redis;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;
using System.Configuration;
using SDAU.GCT.OA.Common;

namespace RedisDemo
{
    class Program
    {
        static void Main(string[] args)
        {
                 RedisClient client = new RedisClient("127.0.0.1", 6379);
            #region Redis支持排序集合
            //  //最后一个参数为我们排序的依据
            //  var s = client.AddItemToSortedSet("12", "百度", 400);

            //client.AddItemToSortedSet("12", "谷歌", 300);
            //client.AddItemToSortedSet("12", "阿里", 200);
            //client.AddItemToSortedSet("12", "新浪", 100);
            //client.AddItemToSortedSet("12", "人人", 500);

            ////升序获取最一个值:"新浪"
            //var list = client.GetRangeFromSortedSet("12", 0, 0);

            //foreach (var item in list)
            //{
            //     Console.WriteLine(item);
            // }

            // //降序获取最一个值:"人人"
            // list = client.GetRangeFromSortedSetDesc("12", 0, 0);

            // foreach (var item in list)
            // {
            //     Console.WriteLine(item);
            // } 
            #endregion


            #region 栈
            //client.PushItemToList("yy", "asd");
            //client.PushItemToList("yy", "asdasd");
            //string str = client.PopItemFromList("yy");
            //Console.WriteLine(str);
            //现金后出
            #endregion




            #region 模拟点击量

            //string hasClientKey = Guid.NewGuid().ToString();
            //if(client.Exists(hasClientKey)==0)
            //{
            //    client.Set(hasClientKey, "already click", TimeSpan.FromDays(1));
            //    client.Increment("count", 1);
            //}

            #endregion



            #region List队列应用
            //client.EnqueueItemOnList("register", "guanchentao");
            //client.EnqueueItemOnList("register", "shaomignjun");
            //client.EnqueueItemOnList("register", "zhangiqng");
            //Console.WriteLine(client.DequeueItemFromList("register")); 

            //先进先出
            #endregion


            //set中元素不重复,但是无序，一般判断某个数据是否在集合中
            //string str = Console.ReadLine();
            //client.AddItemToSet("set2", "1");
            //client.AddItemToSet("set2", "3");
            //client.AddItemToSet("set2", "4");
            //bool b=client.SetContainsItem("set2", str);
            //if(b)
            //{
            //    Console.WriteLine($"{str} is exist");
            //}
            //else
            //{
            //    Console.WriteLine($"{str} id not exsit");
            //}

            //client.AddRangeToSet("rangeset1", new List<string>() { "11", "22", "33" });

            //client.AddItemToSortedSet("SortedSet", "zhang", 3);

            //int setlength = client.GetSortedSetCount("SortedSet");
            // var list = client.GetRangeWithScoresFromSortedSetDesc("SortedSet", 0,setlength);

            //foreach (var item in list)
            //{
            //     Console.WriteLine(item.Key+","+item.Value);
            // }


            #region 模拟热搜排行榜
              for (int i = 0; i < 100; i++)
              {
                  int randomNumber = new Random().Next(1, 4);
                  switch(randomNumber)
                  {
                      case 1: client.IncrementItemInSortedSet("resou", "公鸡下蛋", 1);
                          break;
                      case 2:
                          client.IncrementItemInSortedSet("resou", "猪会上树了", 1);
                          break;
                      case 3:
                          client.IncrementItemInSortedSet("resou", "国庆七天乐", 1);
                          break;
                  }
              }
              int setlength = client.GetSortedSetCount("resou");
              var list = client.GetRangeWithScoresFromSortedSetDesc("resou", 0,setlength);
              foreach (var item in list)
              {
                  Console.WriteLine($"{item.Key}-------点击量{item.Value}");
              }
            //client.BgRewriteAof();
            //client.BgSave();
              Console.Read();
              

            #endregion



            Console.Read();
        }
    }
   
}
